<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构" prop="institutionId">
        <el-select v-model="queryParams.institutionId" filterable placeholder="请选择机构" clearable size="small">
          <el-option
            v-for="dept in deptList"
            :key="dept.institutionId"
            :label="dept.institutionName"
            :value="dept.institutionId"
          />
        </el-select>
      </el-form-item>
     <!-- <el-form-item label="年级id" prop="gradeId">
        <el-input
          v-model="queryParams.gradeId"
          placeholder="请输入年级id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级别名" prop="classCodename">
        <el-input
          v-model="queryParams.classCodename"
          placeholder="请输入班级别名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级状态;对应字典表sys_dictionary表中的Status类型" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择班级状态;对应字典表sys_dictionary表中的Status类型" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="电视上的class_id,动态更新班级名称使用" prop="tvClassId">
        <el-input
          v-model="queryParams.tvClassId"
          placeholder="请输入电视上的class_id,动态更新班级名称使用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新" prop="tvClassNameVersion">
        <el-input
          v-model="queryParams.tvClassNameVersion"
          placeholder="请输入电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新" prop="updateUser">
        <el-input
          v-model="queryParams.updateUser"
          placeholder="请输入电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tongban:class:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tongban:class:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tongban:class:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tongban:class:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="班级id" align="center" prop="classId" />-->
      <el-table-column label="机构名称" align="center" prop="institutionName" />
      <el-table-column label="年级名称" align="center" prop="gradeName" />
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="班级别名" align="center" prop="classCodename" />
<!--      <el-table-column label="班级状态" align="center" prop="status" :formatter="statusFormat" />-->
      <el-table-column label="电视上的class_id" align="center" prop="tvClassId" />
      <el-table-column label="电视上的class_name" align="center" prop="tvClassNameVersion" />
<!--      <el-table-column label="电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新" align="center" prop="createUser" />
      <el-table-column label="电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新" align="center" prop="updateUser" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tongban:class:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tongban:class:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
<!--        <el-form-item label="机构id" prop="institutionId">
          <el-input v-model="form.institutionId" placeholder="请输入机构id" />
        </el-form-item>
        <el-form-item label="年级id" prop="gradeId">
          <el-input v-model="form.gradeId" placeholder="请输入年级id" />
        </el-form-item>-->
        <el-form-item label="机构" prop="institutionId" v-if="institutionShow">
          <el-select v-model="form.institutionId" filterable placeholder="请选择机构" clearable size="small" @change="selectGrade" >
            <el-option
              v-for="dept in deptList"
              :key="dept.institutionId"
              :label="dept.institutionName"
              :value="dept.institutionId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="年级" prop="institutionId" v-if="gradeShow">
          <el-select v-model="form.gradeId" placeholder="请选择机构" clearable size="small" >
            <el-option
              v-for="dept in gradeList"
              :key="dept.gradeId"
              :label="dept.gradeName"
              :value="dept.gradeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="班级名称" prop="className">
          <el-input v-model="form.className" placeholder="请输入班级名称" />
        </el-form-item>
        <el-form-item label="班级别名" prop="classCodename">
          <el-input v-model="form.classCodename" placeholder="请输入班级别名" />
        </el-form-item>
<!--        <el-form-item label="班级状态;对应字典表sys_dictionary表中的Status类型">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>-->
        <el-form-item label="电视上的班级id" prop="tvClassId">
          <el-input v-model="form.tvClassId" placeholder="请输入电视上的班级id" />
        </el-form-item>
        <el-form-item label="电视上的班级名称" prop="tvClassNameVersion">
          <el-input v-model="form.tvClassNameVersion" placeholder="请输入电视上的班级名称" />
        </el-form-item>
<!--        <el-form-item label="电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新" />
        </el-form-item>
        <el-form-item label="电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新" prop="updateUser">
          <el-input v-model="form.updateUser" placeholder="请输入电视上的class_name的版本好,只有云端版本比园所版本号高,才会更新" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClass, getClass, delClass, addClass, updateClass, exportClass } from "@/api/tongban/class";
import {listInstitutionData} from "@/api/tongban/institution";
import {listInstitutionGrade} from "@/api/tongban/grade";

export default {
  name: "Class",
  components: {
  },
  data() {
    return {
      institutionShow: true,
      gradeShow: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班级表格数据
      classList: [],
      deptList: [],
      gradeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 班级状态;对应字典表sys_dictionary表中的Status类型字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        institutionId: null,
        institutionName: null,
        gradeId: null,
        gradeName: null,
        className: null,
        classCodename: null,
        status: null,
        tvClassId: null,
        tvClassNameVersion: null,
        createUser: null,
        updateUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        institutionId: [
          { required: true, message: "机构id不能为空", trigger: "blur" }
        ],
        gradeId: [
          { required: true, message: "年级id不能为空", trigger: "blur" }
        ],
        className: [
          { required: true, message: "班级名称不能为空", trigger: "blur" }
        ],
        classCodename: [
          { required: true, message: "班级别名不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "班级别名不能为空", trigger: "blur" }
        ],
        createUser: [
          { required: true, message: "班级别名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("applets_sys_yes_no").then(response => {
      this.statusOptions = response.data;
    });
    listInstitutionData().then(res => {
      this.deptList = res.data;
    });
  },
  methods: {

    /** 查询年级*/
    selectGrade(id){
      listInstitutionGrade(id).then(res =>{
        this.gradeList = res.data;
        console.log(id);
      })
    },

    /** 查询班级列表 */
    getList() {
      this.loading = true;
      listClass(this.queryParams).then(response => {
        this.classList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 班级状态;对应字典表sys_dictionary表中的Status类型字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        classId: null,
        institutionId: null,
        gradeId: null,
        className: null,
        classCodename: null,
        status: 0,
        tvClassId: null,
        tvClassNameVersion: null,
        createTime: null,
        createUser: null,
        updateTime: null,
        updateUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.classId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.institutionShow = true;
      this.gradeShow = true;
      this.reset();
      this.open = true;
      this.title = "添加班级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.institutionShow = false;
      this.gradeShow = false;
      this.reset();
      const classId = row.classId || this.ids
      getClass(classId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班级";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.classId != null) {
            updateClass(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClass(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const classIds = row.classId || this.ids;
      this.$confirm('是否确认删除班级编号为"' + classIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClass(classIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有班级数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportClass(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
